//
//  ListDetail1ViewController.m
//  JZMCFM
//
//  Created by lanou3g on 15/11/3.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "ListDetail1ViewController.h"
#import "ListDetail1ViewCell.h"
#import "ListDetailModel.h"
#import "ListDetail22ViewController.h"

@interface ListDetail1ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *maintTableView;

//保存数据的数据
@property(nonatomic,strong) NSMutableArray *listMutArray;

@property(nonatomic,strong) NSArray *array;
@end

@implementation ListDetail1ViewController


#pragma mark 懒加载
-(NSMutableArray*)listMutArray{
    
    if (!_listMutArray) {
        _listMutArray = [NSMutableArray array];
    }
    return _listMutArray;
}

-(NSArray*)array{
    return [self.listMutArray copy];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.maintTableView.delegate = self;
    self.maintTableView.dataSource = self;
    
    [self.maintTableView registerNib:[UINib nibWithNibName:@"ListDetail1ViewCell" bundle:nil] forCellReuseIdentifier:@"detail1"];
    [self downLoadFromNetWotk];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ListDetail1ViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail1" forIndexPath:indexPath];
    cell.Serial.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    
    ListDetailModel *model = self.array[indexPath.row];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:model.middleLogo]];
    cell.title.text = model.personDescribe;
    cell.smallTitle.text = model.nickname;
    cell.count.text = [NSString stringWithFormat:@"%ld",model.followersCounts];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}


#pragma mark cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ListDetail22ViewController *listVC = [[ListDetail22ViewController alloc]init];
    ListDetailModel *model = self.array[indexPath.row];
    NSString *KeyID = [NSString stringWithFormat:@"%ld",model.uid];
    listVC.receive = KeyID;
    listVC.picUrl = model.middleLogo;
    listVC.nameTitle = model.nickname;
    [self.navigationController pushViewController:listVC animated:YES];
    
}




#pragma mark 解析数据
-(void)downLoadFromNetWotk{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *string1 = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/";
        NSString *string2 = @"?device=android&key=";
        NSString *string3 = @"&pageId=1&pageSize=40";
        
        string1 = [string1 stringByAppendingString:self.receive1];
        string1 = [string1 stringByAppendingString:string2];
        string1 = [string1 stringByAppendingString:self.receive];
        string1 = [string1 stringByAppendingString:string3];
        //解析网址
        NSURL *url = [NSURL URLWithString:string1];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *listArray = result[@"list"];
            [self.listMutArray removeAllObjects];
            for (NSDictionary *dict  in listArray) {
                ListDetailModel *model = [[ListDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.listMutArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.maintTableView reloadData ];
                
            });
        }];
    });
}




@end
